import {Injectable} from '@angular/core';

@Injectable()
export class ProductService {

  private products: Product[] = [
    new Product(1, '第一件商品', 1.99, 3.5, '这是第一件商品，是我在学习Angular入门实战时创建的', ['电子产品', '图书']),
    new Product(2, '第二件商品', 2.99, 2.5, '这是第二件商品，是我在学习Angular入门实战时创建的', ['电子产品']),
    new Product(3, '第三件商品', 3.99, 4.5, '这是第三件商品，是我在学习Angular入门实战时创建的', ['电子产品', '服装']),
    new Product(4, '第四件商品', 4.99, 1.5, '这是第四件商品，是我在学习Angular入门实战时创建的', ['图书']),
    new Product(5, '第五件商品', 5.99, 2.5, '这是第五件商品，是我在学习Angular入门实战时创建的', ['服装']),
    new Product(6, '第六件商品', 6.99, 3.5, '这是第六件商品，是我在学习Angular入门实战时创建的', ['服装', '图书'])
  ];

  private comments: Comment[] = [
    new Comment(1, 1, '2017-06-03 19:22:22', '张三', 3, '东西不错'),
    new Comment(1, 1, '2017-06-04 20:22:22', '李四', 2, '东西还不错'),
    new Comment(1, 1, '2017-06-05 21:22:22', '王五', 4, '东西挺不错'),
    new Comment(1, 2, '2017-06-06 22:22:22', '赵六', 3.5, '东西确实不错')
  ]

  constructor() {
  }

  getProducts(): Product[] {
    return this.products;
  }

  getProduct(id: number): Product {
    return this.products.find((product) => product.id == id);
  }

  getCommentForProductId(id: number): Comment[] {
    return this.comments.filter((comment: Comment) => comment.productId == id);
  }

}

export class Product {

  constructor(public id: number,
              public title: string,
              public price: number,
              public rating: number,
              public desc: string,
              public categories: Array<string>) {
  }

}

export class Comment {

  constructor(public id: number,
              public productId: number,
              public timeStamp: string,
              public user: string,
              public rating: number,
              public content: string) {
  }

}
